package com.adtech.adms.commons.web.listener;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.adtech.adms.SessionKey;
import com.adtech.adms.system.pojo.User;

/**
 * 
 * 监听用户在线状态
 * @author Dormin 
 * @since 2011-6-7 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class OnlineUserListener implements HttpSessionListener {
	private final static ConcurrentHashMap<String, Object> sessionMap = new ConcurrentHashMap<String, Object>();
	private final static ConcurrentHashMap<String, Object> failIpMap = new ConcurrentHashMap<String, Object>();

	/**
	 * 
	 * @function 注销指定的session
	 * @param sessionId session id
	 * @param reason session失效原因
	 *
	 * @return
	 * @exception
	 *
	 * @author Dormin
	 * @history
	 *
	 */
	public static void invalidSession(String sessionId,String reason){
		HttpSession session = (HttpSession) sessionMap.get(sessionId);
		if(session!=null){
			User user = (User) session.getAttribute(SessionKey.SESSION_KEY_USER);
			session.setAttribute(SessionKey.SESSION_KEY_USER,null);
			session.setAttribute(SessionKey.INVALID_REASON,reason);
//			SystemLogService systemLogService = (SystemLogService) BeanLoader.getBean(session.getServletContext(),Constants.SERVICE_SYSTEM_LOG);
//			systemLogService.saveLogoutLog(user);
			//清除session缓存
			sessionMap.remove(sessionId);
		}
	}

	public void sessionCreated(HttpSessionEvent se) {
		if(se.getSession()!=null) {
			sessionMap.put(se.getSession().getId(), se.getSession());
		}
	}

	public void sessionDestroyed(HttpSessionEvent se) {
		if(se.getSession()!=null) {
			User user = (User) se.getSession().getAttribute(SessionKey.SESSION_KEY_USER);
//			SystemLogService systemLogService = (SystemLogService) BeanLoader.getBean(se.getSession().getServletContext(),Constants.SERVICE_SYSTEM_LOG);
//			systemLogService.saveLogoutLog(user);
			sessionMap.remove(se.getSession().getId());
		}
	}
	/**
	 * 
	 * @function 记录登录失败次数
	 * @param ip 登录ip
	 *
	 * @return
	 * @exception
	 *
	 * @author Dormin
	 * @history
	 *
	 */
	public static void failLogin(String ip){
		IpBlockInfo blockInfo = (IpBlockInfo) failIpMap.get(ip);
		if(blockInfo == null) {
			blockInfo = new IpBlockInfo();
		}
		blockInfo.addTimes();
		if(blockInfo.needBlock()){
			blockInfo.setBlockStartTime(new Date());
		}
		failIpMap.put(ip, blockInfo);
	}
	/**
	 * 
	 * @function 成功登录，清除失败信息
	 * @param ip 登录ip
	 *
	 * @return
	 * @exception
	 *
	 * @author Dormin
	 * @history
	 *
	 */
	public static void successLogin(String ip){
		failIpMap.remove(ip);
	}
	/**
	 * 
	 * @function 检查是否仍需要屏蔽该ip
	 * @param ip 登录ip
	 *
	 * @return
	 * @exception
	 *
	 * @author Dormin
	 * @history
	 *
	 */
	public static boolean checkNeedBlock(String ip){
		IpBlockInfo blockInfo = (IpBlockInfo) failIpMap.get(ip);
		if(blockInfo!=null && blockInfo.needBlock()){
			Date blockStartTime = blockInfo.getBlockStartTime();
			if(blockStartTime!=null){
				Long pastTime = new Date().getTime()-blockStartTime.getTime();
				if(pastTime<SessionKey.IP_BLOCK_TIME*1000){
					return true;
				}
			}
		}
		return false;
	}

}
